package com.jin.boot;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.jin.boot.mapper")
public class SpringBootWebApplication implements CommandLineRunner {

   /* @Autowired
    JdbcTemplate template;*/


    public static void main(String[] args) {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

    @Override
    public void run(String... args) {
    }

   /* private void transCardRealTimeInfoToLiss(){
        List<CardPool> result = Lists.newArrayList();
        String sql = "select msisdn ,prodname as prodName ," +
                "total  as gprsTotal,apnname  from iot_card_real_time_info_20200930\n";
        List<CardPool> lists = template.query(sql, new BeanPropertyRowMapper<>(CardPool.class));
        for (int i = 0; i < lists.size(); i++) {
            CardPool cardPool = lists.get(i);
            if (StringUtils.isNotBlank(cardPool.getProdName())) {
                String prodName = cardPool.getProdName();
                if (StringUtils.isBlank(prodName)){
                    continue;
                }
                String[] prodNameArr = prodName.split(",");
                for (int j = 0; j < prodNameArr.length; j++) {
                    CardPool cardPoolSon = new CardPool();
                    cardPoolSon.setMsisdn(cardPool.getMsisdn());
                    cardPoolSon.setProdName(getIndexItemFromCommonStr(cardPool.getProdName(), j));
                    cardPoolSon.setGprsTotal(getIndexItemFromCommonStr(cardPool.getGprsTotal(), j));
                    cardPoolSon.setApnname(getIndexItemFromCommonStr(cardPool.getApnname(), j));
                    if (StringUtils.isBlank(cardPoolSon.getGprsTotal())){
                        continue;
                    }
                    result.add(cardPoolSon);
                }
            }
        }
       // INSERT INTO ``(`msisdn`, `prodname`, `total`, `apnname`) VALUES ('14765851898', '全国定向流量5元套餐', '30.0', 'CMIOTYCDL.NX');

        String insertSql = "INSERT INTO  iot_card_real_time_info_20200930_list" +
                "( `msisdn`, `prodname`, `total`, `apnname`) VALUES" +
                " (?,?,?,?)";
        for (int i = 0; i < result.size(); i++) {
            Object[] params = new Object[]{
                    result.get(i).getMsisdn(),
                    result.get(i).getProdName(),
                    result.get(i).getGprsTotal(),
                    result.get(i).getApnname()
            };
            template.update(insertSql, params);
            System.out.println("updated -->\t" + i + "/" + result.size());
        }

    }*/


    /**
     *  将iot_card_share_info_202007081739 表的arr数据转为list.
     */
   /* private void transCardShareInfoToList() {
        List<CardPool> result = Lists.newArrayList();
        String sql = "select  `msisdn`, `isFlowShare`, `prodName`, `gprsTotal`, `gprsUsed`, `apnname`  \n" +
                "from iot_card_share_info_20200930";
        List<CardPool> lists = template.query(sql, new BeanPropertyRowMapper<>(CardPool.class));
        for (int i = 0; i < lists.size(); i++) {
            CardPool cardPool = lists.get(i);
            if (StringUtils.isNotBlank(cardPool.getIsFlowShare())) {
                String flowShares = cardPool.getIsFlowShare();
                String[] flowShareArr = flowShares.split(",");
                for (int j = 0; j < flowShareArr.length; j++) {
                    CardPool cardPoolSon = new CardPool();
                    cardPoolSon.setMsisdn(cardPool.getMsisdn());
                    cardPoolSon.setIsFlowShare(flowShareArr[j]);
                    cardPoolSon.setProdName(getIndexItemFromCommonStr(cardPool.getProdName(), j));
                    cardPoolSon.setGprsTotal(getIndexItemFromCommonStr(cardPool.getGprsTotal(), j));
                    cardPoolSon.setGprsUsed(getIndexItemFromCommonStr(cardPool.getGprsUsed(), j));
                    cardPoolSon.setApnname(getIndexItemFromCommonStr(cardPool.getApnname(), j));
                    if (StringUtils.isBlank(cardPoolSon.getGprsTotal())){
                        continue;
                    }
                    result.add(cardPoolSon);
                }
            }
        }
        String insertSql = "INSERT INTO  iot_v2_pool_info_month_9" +
                "( `msisdn`, `isFlowShare`, `prodName`, `gprsTotal`, `gprsUsed`," +
                "`apnname`) VALUES" +
                " (?,?,?,?,?,?)";
        for (int i = 0; i < result.size(); i++) {
            Object[] params = new Object[]{
                    result.get(i).getMsisdn(),
                    result.get(i).getIsFlowShare(),
                    result.get(i).getProdName(),
                    result.get(i).getGprsTotal(),
                    result.get(i).getGprsUsed(),
                    result.get(i).getApnname()
            };
            template.update(insertSql, params);
            System.out.println("updated -->\t" + i + "/" + result.size());
        }

    }*/

    /**
     * 从逗号分隔的字符串中取出第j个元素
     * a,b,c    4
     *
     * @param prodName
     * @param j
     * @return
     */
    private String getIndexItemFromCommonStr(String prodName, int j) {
        if (StringUtils.isBlank(prodName)) {
            return "";
        }
        String[] arr = prodName.split(",");
        if (arr.length > j) {
            return arr[j];
        } else {
            return "";
        }
    }
}
